package cn.edu.hznu.notebook.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.hznu.notebook.entity.Note;
import cn.edu.hznu.notebook.entity.Notebook;
import cn.edu.hznu.notebook.entity.ResponseResult;
import cn.edu.hznu.notebook.service.NoteService;

@RestController
@RequestMapping(value = "/note")
public class NoteController {
	
	@Autowired
	NoteService noteService;
	
	@RequestMapping(value = "/list")
	public List<Note> getNotes(
			@RequestParam(value = "notebookId", required = true) Integer notebookId) {
		Notebook notebook = new Notebook();
		notebook.setId(notebookId);
	    return this.noteService.getNotesByNotebook(notebook);
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseResult addNote(
			@RequestParam(value = "notebookId", required = true) Integer notebookId,
			@RequestParam(value = "title", required = true) String title,
			@RequestParam(value = "content", required = true) String content) {
		ResponseResult result = new ResponseResult();
		result.setSucceeded(true);
		Note note = new Note();
		note.setTitle(title);
		note.setContent(content);
		Notebook notebook = new Notebook();
		notebook.setId(notebookId);
		note.setNotebook(notebook);
		note.setCreateDate(new Date());
		note.setUpdateDate(new Date());
		try {
			this.noteService.addNewNote(note);
		} catch (Exception e) {
			result.setSucceeded(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	public ResponseResult upateNote(
			@PathVariable(value = "id") Integer id,
			@RequestParam(value = "notebookId", required = true) Integer notebookId,
			@RequestParam(value = "title", required = true) String title,
			@RequestParam(value = "content", required = true) String content) {
		ResponseResult result = new ResponseResult();
		result.setSucceeded(true);
		Note note = new Note();
		note.setId(id);
		note.setTitle(title);
		note.setContent(content);
		Notebook notebook = new Notebook();
		notebook.setId(notebookId);
		note.setNotebook(notebook);
		note.setUpdateDate(new Date());
		try {
			this.noteService.updateNoteInfo(note);
		} catch (Exception e) {
			result.setSucceeded(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping(value = "/delete/{id}")
	public ResponseResult deleteNote(@PathVariable(value = "id") Integer id) {
		ResponseResult result = new ResponseResult();
		result.setSucceeded(true);
		try {
			this.noteService.deleteNote(id);
		} catch (Exception e) {
			result.setSucceeded(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
}
